package com.student.management.root.service.impl;

import com.student.management.root.bean.Menu;
import com.student.management.root.bean.Role;
import com.student.management.root.dao.MiddleDao;
import com.student.management.root.dao.RoleDao;
import com.student.management.root.dao.impl.MiddleDaoImpl;
import com.student.management.root.dao.impl.RoleDaoImpl;
import com.student.management.root.service.RoleService;

import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao=new RoleDaoImpl();
    private MiddleDao middleDao=new MiddleDaoImpl();
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        return roleDao.getRoleList(pageIndex, pageSize);
    }

    @Override
    public int total() {
        return roleDao.total();
    }

    @Override
    public int insertRole(String rolename, String state, String[] ids) {
        int k1=0;
        try {
            //1.新增角色表
            Role role = new Role();
            role.setRolename(rolename);
            role.setRolestate(Integer.parseInt(state));
            int k = roleDao.insert(role);
            //2.新增中间表
            //如何获得新增数据的id?
            middleDao.insertMiddle(k,ids);
            k1=1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return k1;
    }

    @Override
    public Role findbyid(int roleid) {
        return roleDao.queryRoleByRid(roleid);
    }

    @Override
    public int updateById(Role role, String[] ids) {
        try {
            int deleteMiddleNum = middleDao.deleteMiddle(role.getRoleid());
            if (deleteMiddleNum>=0){
                System.out.println("成功删除原有middle");
                int middleInsertNum = middleDao.insertMiddle(role.getRoleid(), ids);
                if (middleInsertNum > 0) {
                    System.out.println("成功添加现有middle");
                    return roleDao.updateById(role);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int roleId) {
        return roleDao.delete(roleId);
    }

    @Override
    public int enableRole(int roleid) {
        return roleDao.enableRole(roleid);
    }


}
